var document = this.document;
var window = this.window;
const axios = require('axios');
console.log();

const types = {
    type: 'CUSTOMIZE_LIST_BOX_XJ',
    icon: 'icon-widget-list-viewer',
    title: '自定义列表框',
    version: '1.0.0',
    author: 'XJ王大哥(2357942846)',
    docs: {
        url: 'https://xjwangdage.feishu.cn/wiki/GH69w1MSLiVCetkBZQNcJypxnmg?from=from_copylink',
    },
    isInvisibleWidget: false,
    isGlobalWidget: false,
    properties: [
        {
            key: 'data',
            label: '数据',
            valueType: 'string',
            defaultValue: `["a", "b", "c"]`,
            editorType: 'TextArea',
        },
        {
            key: 'html',
            label: 'HTML',
            valueType: 'string',
            defaultValue: '<p>本项内容是：${item}，是第${index}个元素</p>\n<!--如代码所示,此处支持2种插入符-->',
            editorType: 'TextArea',
        },
        {
            key: 'css',
            label: 'CSS',
            valueType: 'string',
            defaultValue: '#${ID} > div { color: red }\n/*${ID}插入符表示本控件的ID*/',
            editorType: 'TextArea',
        },
        {
            key: 'js',
            label: 'JS',
            valueType: 'string',
            defaultValue:
                'widget.data.push("666")\n// 用JS对数据进行处理,widget指向控件的this环境,可以操作控件属性、读取信息等',
            editorType: 'TextArea',
        },
        {
            key: 'combinationPackage',
            label: '或粘贴组合包',
            valueType: 'string',
            defaultValue: '',
            editorType: 'TextArea',
        },
    ],
    methods: [
        {
            key: 'generateCombinationPackage',
            label: '生成组合包',
            params: [],
            valueType: 'string',
        },
    ],
    events: [
        // { key: `onclick`, label: `被点击`, params: [{ key: 'number', label: '行', valueType: 'number' }] },
        {
            key: 'error',
            label: '出错',
            params: [{ key: 'error', label: '信息', valueType: 'string' }],
        },
    ],
};

class Widget extends VisibleWidget {
    constructor(p) {
        super(p);
        Object.assign(this, p);

        if (typeof this.data !== 'object') {
            try {
                this.data = JSON.parse(this.data);
            } catch (e) {
                this.data = [];
            }
        }

        if (this.combinationPackage) Object.assign(this, JSON.parse(this.combinationPackage));

        try {
            new Function('widget', this.js)(this);
        } catch (e) {}
    }

    generateCombinationPackage = () => JSON.stringify({ html: this.html, css: this.css, js: this.js });

    render() {
        try {
            return (
                <>
                    {this.data?.map((item, index) => (
                        <div
                            dangerouslySetInnerHTML={{
                                __html: new Function('item', 'index', `return \`${this.html}\``)(item, index),
                            }}
                        />
                    ))}
                    <style
                        dangerouslySetInnerHTML={{
                            __html: `${new Function('ID', `return \`${this.css}\``)(this.__widgetId)}`,
                        }}></style>
                </>
            );
        } catch (e) {
            this.emit('error', e);
        }
    }
}

// prettier-ignore
{/** lightBlue积木 **/Widget.prototype.concat=function(d){this.setProps({data:this.data.concat(d)})};Widget.prototype.push=function(d){this.data.push(d);this.setProps({data:this.data})};Widget.prototype.splice=function(s,d){this.data.splice(s,0,d);this.setProps({data:this.data})};Widget.prototype.pop=function(){this.data.pop();this.setProps({data:this.data})};Widget.prototype.remove=function(s){this.data.splice(s-1,1);this.setProps({data:this.data})};Widget.prototype.replace=function(s,d){this.data.splice(s-1,1,d);this.setProps({data:this.data})};Widget.prototype.replaceKey=function(s,k,v){this.data[s-1][k]=v;this.setProps({data:this.data})};Widget.prototype.empty=function(){this.setProps({data:[]})};Widget.prototype.inquire=function(s){return this.data[s-1]};Widget.prototype.inquireKey=function(s,k){return this.data[s-1][k]};Widget.prototype.length=function(){return this.data.length};Widget.prototype.scrollTo=function(s){this.lightBluePE.scrollTo({top:this.lightBlueCE[s-1].offsetTop})};Widget.prototype.scrollToStart=function(){this.lightBluePE.scrollTop=0};Widget.prototype.scrollToEnd=function(){this.lightBluePE.scrollTop=this.lightBluePE.scrollHeight};Widget.prototype.scrollTopNear=function(){let st=this.lightBluePE.scrollTop;let index=0;for(let i of this.lightBlueCE){index++;if(i.offsetTop>=st)break}return index};Widget.prototype.scrollBottomNear=function(){let st=this.lightBluePE.scrollTop+this.lightBluePE.clientHeight;let index=0;for(let i of this.lightBlueCE){index++;if(i.offsetTop+i.offsetHeight>=st)break}return index};types.methods.push(...[{key:"concat",label:"添加",params:[{key:"data",label:"列表",valueType:["string","number","boolean","array","color","object",],defaultValue:"",},],blockOptions:{callMethodLabel:false,line:"写入"},},{key:"push",label:"添加",params:[{key:"data",label:"单行",valueType:["string","number","boolean","array","color","object",],defaultValue:"",},],blockOptions:{callMethodLabel:false},},{key:"splice",label:"插入",params:[{key:"start",label:"第",labelAfter:"行后",valueType:"number",defaultValue:0,},{key:"data",label:"",valueType:["string","number","boolean","array","color","object",],defaultValue:"",},],blockOptions:{callMethodLabel:false},},/**  本轻蓝超级框架由XJ王大哥(QQ2357942846)制作 **/{key:"pop",label:"删除末尾行",params:[],blockOptions:{callMethodLabel:false},},{key:"remove",label:"删除",params:[{key:"start",label:"第",labelAfter:"行",valueType:"number",defaultValue:0,},],blockOptions:{callMethodLabel:false},},{key:"replace",label:"替换",params:[{key:"start",label:"第",labelAfter:"行为",valueType:"number",defaultValue:0,},{key:"data",label:"",valueType:["string","number","boolean","array","color","object",],defaultValue:"",},],blockOptions:{callMethodLabel:false},},{key:"replaceKey",label:"替换",params:[{key:"start",label:"第",labelAfter:"行",valueType:"number",defaultValue:0,},{key:"key",label:"键",labelAfter:"为",valueType:["string","number","boolean","array","color","object",],defaultValue:"",},{key:"value",label:"",valueType:["string","number","boolean","array","color","object",],defaultValue:"",},],blockOptions:{callMethodLabel:false},},{key:"empty",label:"清空",params:[],blockOptions:{callMethodLabel:false},},{key:"inquire",params:[{key:"start",label:"第",labelAfter:"行的值",valueType:"number",defaultValue:0,},],valueType:["string","number","boolean","array","color","object"],blockOptions:{callMethodLabel:false,line:"读取"},},{key:"inquireKey",params:[{key:"start",label:"第",labelAfter:"行",valueType:"number",defaultValue:0,},{key:"key",label:"键",labelAfter:"的值",valueType:["string","number","boolean","array","color","object",],defaultValue:"",},],valueType:["string","number","boolean","array","color","object"],blockOptions:{callMethodLabel:false},},{key:"length",label:"行数",params:[],valueType:"number",blockOptions:{callMethodLabel:false},},{key:"scrollTo",label:"滚动至",params:[{key:"start",label:"第",labelAfter:"行",valueType:"number",defaultValue:0,},],blockOptions:{color:"rgba(0, 175, 195, 1)",callMethodLabel:false,line:"滚动",},},{key:"scrollToStart",label:"滚动至顶端",params:[],blockOptions:{color:"rgba(0, 175, 195, 1)",callMethodLabel:false},},{key:"scrollToEnd",label:"滚动至底端",params:[],blockOptions:{color:"rgba(0, 175, 195, 1)",callMethodLabel:false},},{key:"scrollTopNear",label:"可见区域顶端行",valueType:"number",params:[],blockOptions:{color:"rgba(0, 175, 195, 1)",callMethodLabel:false},},{key:"scrollBottomNear",label:"可见区域底端行",valueType:"number",params:[],blockOptions:{color:"rgba(0, 175, 195, 1)",callMethodLabel:false},},]);/**  本轻蓝超级框架由XJ王大哥(QQ2357942846)制作 **/if(0){let ind=0;for(let i in lightBlue生成){ind++;types.methods.push({key:`快速${i}`,label:`快速${i}`,valueType:["string","number","boolean","array","color","object"],params:lightBlue生成[i],blockOptions:{color:"rgb(249, 204, 55)",callMethodLabel:false},});Widget.prototype[`快速${i}`]=(...a)=>{let LBdata={};lightBlue生成[i].map((item,index)=>(LBdata[item.key]=a[index]));return LBdata};if(ind===1)types.methods[types.methods.length-1].blockOptions.line="快速生成"}}}

for (let i of types.methods) i.blockOptions = { ...i.blockOptions, callMethodLabel: false };

exports.types = types;
exports.widget = Widget;
